//
//  BrandViewController.m
//  App-MyFairldy
//
//  Created by 客官大大 on 15/5/23.
//  Copyright (c) 2015年 客官大大. All rights reserved.
//

#import "BrandViewController.h"


@interface BrandViewController ()

@end

@implementation BrandViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    activity.color = [UIColor blackColor];
    activity.center = self.view.center;
    [self.view addSubview:activity];
    [activity startAnimating];
    [activity release];
    [self downLoadThisPageData];
}

- (void)initCollectionView
{
    UICollectionViewFlowLayout *brandLayout = [[UICollectionViewFlowLayout alloc] init];
    // 每个小方块的大小
    brandLayout.itemSize = CGSizeMake(100, 100);
    // 最小行距
    brandLayout.minimumLineSpacing = 40;
    // 最小列距
    brandLayout.minimumInteritemSpacing = 15;
    // 上下左右边距
    brandLayout.sectionInset = UIEdgeInsetsMake(10, 13, 10, 30);
    
    brandCollectView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, 375, 667 - 64) collectionViewLayout:brandLayout];
    brandCollectView.delegate = self;
    brandCollectView.dataSource = self;
    // 注册cell
    [brandCollectView registerClass:[BrandCollectionViewCell class]forCellWithReuseIdentifier:@"cell"];
    brandCollectView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:brandCollectView];
}

- (void)downLoadThisPageData
{
    NSString *str = @"http://food.boohee.com/fb/v1/categories?kind=brand";
    str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    // 创建网址
    NSURL *url = [NSURL URLWithString:str];
    // 创建请求request
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];
    // 系统默认是GET请求
    request.HTTPMethod = @"GET";
    // 发送请求异步block
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *array = [dic objectForKey:@"categories"];
        self.arrayBrand = [NSMutableArray array];
        for (NSDictionary *dic1 in array) {
            BrandModel *model = [BrandModel getBrandModel];
            model.name = [dic1 objectForKey:@"name"];
            model.IconImage = [dic1 objectForKey:@"image_url"];
            [self.arrayBrand addObject:model];
            [activity stopAnimating];
            [self initCollectionView];
        }
    }];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.arrayBrand.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    BrandCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor clearColor];
    BrandModel *model = [self.arrayBrand objectAtIndex:indexPath.row];
    cell.nameLabel.text = model.name;
    
    NSURL *url = [NSURL URLWithString:model.IconImage];
    NSData *data = [NSData dataWithContentsOfURL:url];
    UIImage *image = [UIImage imageWithData:data];
    cell.iconImage.image = image;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%ld", indexPath.row);

   
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
